package com.nsgf.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.ChartDataBean;
import com.nsgf.library.data.bean.DisplacementBean;
import com.nsgf.library.data.bean.TransfusionBean;
import com.nsgf.library.databinding.ItemMarkerChartsBinding;
import com.nsgf.library.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
@SuppressLint("ViewConstructor")
public class DisplacementMarkerListView extends MarkerView {

    private List<DisplacementBean> list = new ArrayList<>();
    private LinearLayout ll_list_marker_charts;

    private List<ChartDataBean> chartDataBeans;
    private Context mContext;
    private TextView tv_date;

    private int type = 0;//0渗流监测 1渗压监测

    private boolean many;//多图例
    private int index;//多图例

    public DisplacementMarkerListView(Context context,
                                      List<DisplacementBean> list,
                                      List<ChartDataBean> chartDataBeans,
                                      int index) {
        super(context, R.layout.transfusion_marker_view);

        this.mContext = context;
        this.list = list;
        tv_date = findViewById(R.id.tv_date);
        ll_list_marker_charts = findViewById(R.id.ll_list_marker_charts);
        this.chartDataBeans = chartDataBeans;
        this.index = index;
    }

    public DisplacementMarkerListView(Context context,
                                      List<DisplacementBean> list,
                                      List<ChartDataBean> chartDataBeans, boolean many, int index) {
        super(context, R.layout.transfusion_marker_view);

        this.mContext = context;
        this.list = list;
        tv_date = findViewById(R.id.tv_date);
        ll_list_marker_charts = findViewById(R.id.ll_list_marker_charts);
        this.chartDataBeans = chartDataBeans;
        this.many = many;
        this.index = index;
    }


    /**
     * 刷新图例数据
     * @param chartDataBeans
     */
    public void setChartDataBeans(List<ChartDataBean> chartDataBeans) {
        this.chartDataBeans = chartDataBeans;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        ll_list_marker_charts.removeAllViews();

        if (many) {

            for (int i = 0; i < chartDataBeans.size(); i++) {
                if (chartDataBeans.get(i).isChartLineDataSet()) {

                    if ((int) e.getX() < list.get(i).getBeanList().size()) {
                        tv_date.setText(Utils.formatTimeYMD(list.get(i).getBeanList().get((int) e.getX()).getTime()));
                        View view = View.inflate(mContext, R.layout.item_marker_charts, null);
                        ItemMarkerChartsBinding chartsBinding = DataBindingUtil.bind(view);
                        //展示自定义X轴值 后的X轴内容
                        GradientDrawable drawable = new GradientDrawable();
                        drawable.setShape(GradientDrawable.OVAL);
                        drawable.setColor(chartDataBeans.get(i).getColor());
                        chartsBinding.viewColor.setBackground(drawable);

                        if (index == 0) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacementNSrc());
                        } else if (index == 1) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacementESrc());
                        } else if (index == 2) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacement() + "mm");
                        } else if (index == 3) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacement() + "");
                        } else if (index == 4 || index == 9) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacementSrc());
                        } else if (index == 6) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacementNSrc());
                        } else if (index == 7) {
                            chartsBinding.tvValue.setText(list.get(i).getBeanList().get((int) e.getX()).getDisplacementESrc());
                        }
                        chartsBinding.tvTitle.setText(chartDataBeans.get(i).getChartTitles());
                        ll_list_marker_charts.addView(view);
                    }

                }
            }
        } else {

            tv_date.setText(list.get((int) e.getX()).getTestPoint());
            for (int i = 0; i < chartDataBeans.size(); i++) {
                if (chartDataBeans.get(i).isChartLineDataSet()) {
                    if ((int) e.getX() < list.size()) {

                        View view = View.inflate(mContext, R.layout.item_marker_charts, null);
                        ItemMarkerChartsBinding chartsBinding = DataBindingUtil.bind(view);
                        //展示自定义X轴值 后的X轴内容
                        GradientDrawable drawable = new GradientDrawable();
                        drawable.setShape(GradientDrawable.OVAL);
                        drawable.setColor(chartDataBeans.get(i).getColor());
                        chartsBinding.viewColor.setBackground(drawable);

                        if (index == 0) {
                            //位移监测 累计水平位移量分布图
                            chartsBinding.tvValue.setText((i == 0
                                    ? list.get((int) e.getX()).getDisplacementN()
                                    : list.get((int) e.getX()).getDisplacementE()) + "");
                        } else if (index == 2 || index == 1) {
                            chartsBinding.tvValue.setText(list.get((int) e.getX()).getDisplacement() + "");
                        } else if (index == 3) {
                            chartsBinding.tvValue.setText(list.get((int) e.getX()).getDisplacement() + "");
                        } else if (index == 5) {
                            //GNSS 累计水平位移量分布图
                            chartsBinding.tvValue.setText((i == 0
                                    ? list.get((int) e.getX()).getDisplacementN()
                                    : list.get((int) e.getX()).getDisplacementE()) + "");
                        } else if (index == 8) {
                            chartsBinding.tvValue.setText(list.get((int) e.getX()).getDisplacement() + "");
                        }
                        chartsBinding.tvTitle.setText(chartDataBeans.get(i).getChartTitles());
                        ll_list_marker_charts.addView(view);
                    }
                }
            }
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
